# coding: utf-8
# 文件名称: 2. 界面自动化_发送话术.py
# 创建时间: 2021/7/1 0:07
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
import json
import csv
import random




def main():
    chromeOptions = webdriver.ChromeOptions()
    chromeOptions.add_argument(r"user-data-dir=C:\Users\蓝云\AppData\Local\Google\Chrome\User Data")  # 把数据传入程序
    chromeOptions.add_experimental_option('excludeSwitches', ['enable-automation'])  # 防止网站发现我们使用模拟器
    # driver_path = r'D:\Edgedriver\edgedriver_win64\msedgedriver.exe'
    driver_path = r'D:\chromedriver\chromedriver.exe'
    driver = webdriver.Chrome(options=chromeOptions,executable_path=driver_path)
    driver.get('http://mobile.yangkeduo.com/goods.html?goods_id=223203929667')

    time.sleep(3)


    with open('pdd.csv', 'r', encoding='utf-8') as f:
        reader = csv.reader(f)
        i = 0
        for r in reader:
            goods_id = r[0]
            mall_id = r[1]
            jiage = r[2]
            url = f'https://mobile.yangkeduo.com/chat_detail.html?mall_id={mall_id}&goods_id={goods_id}&cat_id_1=16901&page_from=0&is_full_reduction=false&is_full_return=false&refer_page_name=goods_detail&refer_page_id=10014_1625074923468_7n5946ogyw&refer_page_sn=10014'
            driver.get(url)
            time.sleep(1)

            actions = ActionChains(driver)  # 构建行为链
            input_tag = driver.find_element_by_id('input-content')
            actions.move_to_element(input_tag)  # 把鼠标移动到文本框
            time.sleep(1)
            actions.send_keys_to_element(input_tag, f'您好，请问这个{jiage}的商品支持好评返现1元吗？')  # 在文本框输入数据
            # input_tag.send_keys(f'您好，请问这个{jiage}的商品支持好评返现1元吗？')
            actions.perform()  # 执行行为链
            time.sleep(2)

            actions = ActionChains(driver)  # 构建行为链
            submit_tag = driver.find_element_by_class_name('send-button')
            actions.move_to_element(submit_tag)  # 把鼠标移动到按钮上面
            actions.click(submit_tag) # 单击按钮
            actions.perform()  # 执行行为链



            i += 1
            print(f'第 {i} 个商品发送完成，ID是 {goods_id}')
            time.sleep(10)


if __name__ == '__main__':
    main()



